<template>
  <div class="bottom-bar">
    <div class="all-checked">
      <check-button
        class="check-button"
        :is-checked="isSelectAll"
        @click.native="checkClick"
      />
      <span>全选</span>
    </div>

    <div class="price">合计:{{ totalPrice }}</div>

    <div class="calculate" @click="calcClick">去支付({{ checkLength }})</div>
  </div>
</template>

<script>
import CheckButton from "components/content/checkButton/CheckButton";
import { mapGetters,} from "vuex";

export default {
  name: "CartBottomBar",
  // data(){
  //   return{
  //     message:'asd'
  //   }
  // },
  components: {
    CheckButton,
  },
  computed: {
    totalPrice() {
      return (
        "￥" +
        this.cartList
          .filter((item) => {
            return item.checked;
          })
          .reduce((preValue, item) => {
            return preValue + item.price * item.count;
          }, 0)
          .toFixed(2)
      );
    },
    //商品个数
    checkLength() {
      return this.cartList
        .filter((item) => item.checked)
        .reduce((preValue, item) => {
          return preValue + item.count;
        }, 0);
    },
    ...mapGetters(["cartList"]),
    isSelectAll() {
      if ((this, this.cartList.length === 0)) {
        return false;
      }
      return !this.cartList.some((item) => {
        return !item.checked;
      });
    },
  },
  methods: {
    checkClick() {
      if (this.isSelectAll) {
        //全选
        this.cartList.forEach((item) => (item.checked = !item.checked));
      } else {
        this.cartList.forEach((item) => (item.checked = true));
      }
    },
    calcClick() {
      if ( this.checkLength == 0) {
        // console.log(this.$toast);
        this.$toast.show("请选择购买的商品", 2000);
      }
    },
  },
  // created(){
  //   console.log(this.$store.cartList);
  //   console.log(this.message);
  // }
};
</script>

<style scoped>
.bottom-bar {
  line-height: 40px;
  height: 40px;
  display: flex;
  background-color: #eee;
  align-items: center;
  font-size: 14px;
}

.all-checked {
  display: flex;
  align-items: center;
  margin-left: 10px;
  line-height: 20px;
  width: 60px;
}
.check-button {
  width: 20px;
  height: 20px;
  margin-right: 5px;
}
.price {
  margin-left: 20px;
  flex: 1;
}

.calculate {
  width: 90px;
  background-color: red;
  color: #fff;
  text-align: center;
}
</style>
